<?php
class IndexAction extends Action{
	
	//加载简单注册页面并获取省份信息
	public function registerForm(){
		$province = M('Province');
		$provincelist = $province->query("select Id,Name,ZoneId from __TABLE__");
		$this->assign('province',$provincelist);
		$this->display();
	}
	//获取高校列表
	public function getSchoolList(){
		$id = $_GET['pro'];
		$highschool = M('Highschool');
		$highschoollist = $highschool->query("select Id,SchoolName from __TABLE__ where ProvinceId={$id}");
		echo json_encode($highschoollist);
	}
	//用户名验证
	public function checkUsername(){
		$username = $_POST['UserName'];
		//$onename = M('Users')->where("UserName = {$username}")->select();
		$onename = M('Users')->query("select UserId from __TABLE__ where UserName='{$username}'");
		if($onename){
			echo '1';
		}else{
			echo '0';
		}
    }
	public function registerCom(){
		$form['UserType'] = 3;
		$form['UserName'] = trim($this->_post('UserName'));
		$form['OrgLocation'] = $this->_post('OrgLocationId');
		$form['SchoolId'] = $this->_post('SchoolId');
		$form['OrgName'] = $this->_post('OrgName');
		$form['TrueName'] = $this->_post('TrueName');
		$form['Password'] = md5(trim($this->_post('Password')));
		$form['PhoneNumber'] = $this->_post('PhoneNumber');
		$form['Email'] = $this->_post('Email');
		$form['CreateDate'] = date('Y-m-d H:i:s');
		$form['IsApproved'] = 0;
		 $add = create_user($form);
		$_SESSION[userId]=$add;
		if($add){
			$userId = $add;
			//操作teacher表
			$teacher = D('Teacher');
			$result = $teacher->create();
			if (!!$result){
				// 如果创建失败 表示验证没有通过 输出错误提示信息
				echo '数据写入失败，请与我们联系';

			}else{
				// 验证通过 进行其他数据操作
				$teacher->execute("insert into __TABLE__ (UserId) values ('{$userId}')");
				$this->success('基本信息已提交', "detailForm");
			}
		}else{
			$this->error('提交失败');
		 }
	}
	public function detailForm(){
		$this->display();
	}
	//老师详细信息注册
	public function detailFormCom(){
	    $uid = $_SESSION['userId'];
		$data=array(
				"EduLevel" => "{$this->_post('DropDownList1')}",
				"DepartmentName" => "{$this->_post('Department')}",
				"Job" => "{$this->_post('Job')}",
				"Title" => "{$this->_post('Professional')}",
				"Address" => "{$this->_post('Address')}-{$this->_post('Address1')}-{$this->_post('Address2')}-{$this->_post('Address3')}",
				"PostalCode" => "{$this->_post('PostalCode')}",
				"FixedPhone" => "{$this->_post('Telphone')}-{$this->_post('Tel_number')}-{$this->_post('Tel_ext')}",
				"OtherPhone" => "{$this->_post('OtherPhone')}",
				"MSN" => "{$this->_post('MSN')}",
				"QQ" => "{$this->_post('QQ')}",
				"Fetion" => "{$this->_post('Fetion')}",
				"PersonalIntroduce" => "{$this->_post('PersonalIntroduce')}",
			);
		$m = M('teacher')->where("UserId={$uid}")->setField($data);
		if($m){
			$this->success('注册成功，请等待审核',"__APP__/");
		}else{
			$this->error('提交失败');
		}
	}
	public function agreement(){
		$this->display();
	}
	//添加修改基本信息页面
	public function editRegister(){
		$uid = $_SESSION['userId'];
		$user = M('Users')->where("UserId={$uid}")->field('UserName,TrueName,PhoneNumber,Email')->find();
		$this->assign('user',$user);
		//加载高校所在地
		$province = M('Province');
		$provincelist = $province->query("select Id,Name,ZoneId from __TABLE__");
		$this->assign('province',$provincelist);
		$this->display();
		
	}
	//更新修改后的基本信息
	public function editRegisterForm(){
		$uid = $_SESSION['userId'];
		$pass = md5(trim($this->_post('Password')));
		$data=array(
			"OrgLocation" => "{$this->_post('OrgLocationId')}",
			"SchoolId" => "{$this->_post('SchoolId')}",
			"OrgName" => "{$this->_post('OrgName')}",
			"Password" => "{$pass}",
			"TrueName" => "{$this->_post('TrueName')}",
			"PhoneNumber" => "{$this->_post('PhoneNumber')}",
			"Email" => "{$this->_post('Email')}",
		);
		edit_base_info($uid,$data);
		$this->success('基本信息已修改', "detailForm");
	}
}